package com.mybatis.mybatisdatab.util;

import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.UUID;

public class RandomUtil {

    public static final int RANDOM_TYPE_UUID = 0;
    public static final int RANDOM_TYPE_NUM = 1;
    public static final int RANDOM_TYPE_LOW = 2;
    public static final int RANDOM_TYPE_NUM_LOW = 3;
    public static final int RANDOM_TYPE_UP = 4;
    public static final int RANDOM_TYPE_NUM_UP = 5;
    public static final int RANDOM_TYPE_LOW_UP = 6;
    public static final int RANDOM_TYPE_NUM_LOW_UP = 7;
    private static final String ALL_NUM = "0123456789";
    private static final String LOW_CASE = "abcdefghijklmnopqrstuvwxyz";
    private static final String UP_CASE = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    private static final List<String> STRINGS = Arrays.asList("0123456789", "abcdefghijklmnopqrstuvwxyz", "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
    private static final int RANDOM_NUM_LENGTH = 11;
    private static Random r = new Random();

    private RandomUtil() {
    }

    //获取uuid
    public static String uuid() {
        return UUID.randomUUID().toString().replace("-", "");
    }

    public static String getRandom(int length, int type) {
        if (type == 0) {
            return uuid().substring(0, length);
        } else {
            StringBuilder buffer = new StringBuilder(26);
            StringBuilder sb = new StringBuilder(length);

            int range;
            for(range = 0; range < STRINGS.size(); ++range) {
                if ((type & 1 << range) == 1 << range) {
                    buffer.append((String)STRINGS.get(range));
                }
            }

            range = buffer.length();

            for(int i = 0; i < length; ++i) {
                sb.append(buffer.charAt(r.nextInt(range)));
            }

            return sb.toString();
        }
    }

    public static String getRandom() {
        long l = r.nextLong();
        String str = Long.toUnsignedString(l);
        return fillZero(str);
    }

    private static String fillZero(String str) {
        return str.length() >= 11 ? str.substring(0, 11) : fillZero("0" + str);
    }

    //创建随机的6位数
    public static String createCode() {
        return getRandom(6, 1);
    }

//    public static String getRandomFileName() {
//        String y = TimeUtil.nowTimeInt();
//        return y + r.nextInt();
//    }

}
